package com.sqfw.project.zj.controller;

import com.sqfw.common.utils.SecurityUtils;
import com.sqfw.common.utils.StringUtils;
import com.sqfw.framework.web.controller.BaseController;
import com.sqfw.framework.web.domain.AjaxResult;
import com.sqfw.framework.web.page.TableDataInfo;
import com.sqfw.project.system.domain.SysUser;
import com.sqfw.project.system.service.ISysUserService;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.service.IWpInfoService;
import com.sqfw.project.zj.domain.ZjSoundCase;
import com.sqfw.project.zj.domain.ZjSoundCollect;
import com.sqfw.project.zj.service.IZjSoundCaseService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 质检声音案例Controller
 *
 * @author sudneghui
 * @date 2023-06-27
 */
@Api(tags = "质检声音案例")
@RestController
@RequestMapping("/zj/sound_case")
public class ZjSoundCaseController extends BaseController {


    @Autowired
    private IZjSoundCaseService zjSoundCaseService;

    @Autowired
    private IWpInfoService wpInfoService;

    @Autowired
    private ISysUserService userService;



    /**
     * 查询声音案例列表
     * （声音案例待受理查询页面列表）
     *
     * @param zjSoundCase 声音案例
     * @return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    @GetMapping("/list")
    public TableDataInfo qryZjSoundCaseList(ZjSoundCase zjSoundCase) throws ParseException {

        if (StringUtils.isNotEmpty(zjSoundCase.getSubmitName())){
            List<SysUser> sysUsers = userService.selectUserByNickName(zjSoundCase.getSubmitName());
            if (sysUsers.size() > 0){
                SysUser sysUser = sysUsers.stream().findFirst().orElse(null);
                zjSoundCase.setCreateBy(sysUser.getCode());

            }else {
                return getDataTable(new ArrayList<>());
            }
        }

        startPage();
        List<ZjSoundCase> list = zjSoundCaseService.qryZjSoundCaseList(zjSoundCase);
        return getDataTable(list);
    }

    /**
     * 查询已采纳声音案例列表
     *
     * @param zjSoundCase 声音案例
     * @return TableDataInfo
     */
    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    @GetMapping("/accepted_sound_cases")
    public TableDataInfo qryAcceptedZjSoundCaseList(ZjSoundCase zjSoundCase) throws ParseException {
        startPage();
        List<ZjSoundCase> list = zjSoundCaseService.qryAcceptedZjSoundCaseList(zjSoundCase);
        return getDataTable(list);
    }

    /**
     * 查询我的声音案例列表
     * （展示不采纳和待采纳）
     *
     * @return 声音案例集合
     */
    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    @GetMapping("/my_sound_cases")
    public TableDataInfo qryMyZjSoundCaseList(ZjSoundCase zjSoundCase) {
        startPage();
        //获取工号
        String code = SecurityUtils.getLoginUser().getUser().getCode();
        zjSoundCase.setSender(code);
        List<ZjSoundCase> list = zjSoundCaseService.qryMyZjSoundCaseList(zjSoundCase);
        return getDataTable(list);
    }

    /**
     * 获取声音案例详细信息
     *
     * @param sid 声音案例id
     */
    @GetMapping(value = "/{sid}")
    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    public AjaxResult qryZjSoundCaseBySid(@PathVariable("sid") Long sid) {
        return success(zjSoundCaseService.qryZjSoundCaseBySid(sid));
    }


    /**
     * 处理声音案例
     */
    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    @PostMapping("/handleZjSoundCase")
    public AjaxResult handleZjSoundCase(@RequestBody ZjSoundCase zjSoundCase) {
        return toAjax(zjSoundCaseService.updateZjSoundCase(zjSoundCase));
    }


    /**
     * 添加声音案例
     */
    @PreAuthorize("@ss.hasAnyPermi('zj:sound_case,addZjSoundCase')")
    @PostMapping("/addZjSoundCase")
    public AjaxResult addZjSoundCase(@RequestBody WpInfo wpInfo) {
        if (StringUtils.isNotEmpty(wpInfo.getIsSoundCases()) && "是".equals(wpInfo.getIsSoundCases())) {
            WpInfo info = null;
            if (StringUtils.isEmpty(wpInfo.getScid()) && StringUtils.isNotEmpty(wpInfo.getWpid())) {
                info = wpInfoService.selectWpInfoByWpid(wpInfo.getWpid());
            }

            if (StringUtils.isNotEmpty(wpInfo.getScid()) && StringUtils.isEmpty(wpInfo.getWpid())) {
                info = wpInfoService.selectWpInfoByScid(wpInfo.getScid());
            }
            if (info == null) {
                return AjaxResult.error("该录音无工单编号，无法添加案例");
            }

            // 添加声音案例
            ZjSoundCase zjSoundCase = new ZjSoundCase();
            zjSoundCase.setWpid(info.getWpid());
            zjSoundCase.setAcceptStatus("待采纳");
            zjSoundCase.setIsSoundCases("是");
            String code = SecurityUtils.getLoginUser().getUser().getCode();
            zjSoundCase.setCreateBy(code);
            zjSoundCase.setCreateTime(new Date());
            zjSoundCase.setSender(info.getSender());
            zjSoundCase.setSubmitReason(wpInfo.getSubmitReason());
            if (StringUtils.isNotEmpty(wpInfo.getZjComment())){
                zjSoundCase.setZjComment(wpInfo.getZjComment());
            }



            info.setIsSoundCases("是");
            info.setSubmitReason(info.getSubmitReason());
            wpInfoService.updateWpInfoSoundCase(info);
            return toAjax(zjSoundCaseService.addZjSoundCase(zjSoundCase));
        }
        return AjaxResult.error("参数异常，无法添加案例");
    }

    /**
     * 收藏，取消收藏
     *
     * @param zjSoundCase
     * @return
     */
    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    @PostMapping("/updateCollectStatus")
    public AjaxResult updateCollectStatus(@RequestBody ZjSoundCase zjSoundCase) {
        if (StringUtils.isEmpty(zjSoundCase.getCollectStatus())) {
            return AjaxResult.error("参数有误！");
        }
        zjSoundCaseService.updateCollectStatus(zjSoundCase);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('zj:sound_case')")
    @PostMapping("/updateCollectDelFlag")
    public AjaxResult updateCollectDelFlag(@RequestBody ZjSoundCase zjSoundCase) {
        zjSoundCaseService.updateCollectDelFlag(zjSoundCase);
        return AjaxResult.success();
    }

}
